package com.loveprogrammer.model;

import com.loveprogrammer.constants.Tuple;
import com.loveprogrammer.engine.AttackResult;
import com.loveprogrammer.engine.battle.Attackable;
import com.loveprogrammer.engine.equipment.WearEquipment;
import com.loveprogrammer.engine.enums.EquipmentEnum;
import com.loveprogrammer.engine.enums.LevelExpEnum;
import com.loveprogrammer.engine.enums.MonsterQualityEnum;
import com.loveprogrammer.factory.template.JobTemplate;
import com.loveprogrammer.model.goods.Equipment;
import com.loveprogrammer.model.goods.Equips;
import com.loveprogrammer.utils.ConsoleColors;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class Character implements Attackable, WearEquipment, Serializable {

    // 装备的装备
    private Equips equips;


    protected int id;                         // 角色id
    protected String name;                   // 角色信息模板
    // protected int attack;                    // 攻击力
    protected int strength;                  // 力量 影响物理输出 物理技能输出
    protected int armature;                 // 护甲值 影响物理防御和法术防御
    protected int constitution;               // 体质 影响生命值 一点体质增加10点生命值
    protected int magic;                       // 魔力 影响法术输出 法术技能输出
    protected int technique;                   // 技巧 影响闪避率、暴击率
    protected int speed;                         // 攻击速度

    protected AtomicInteger hp;                 // 生命值 必须是一个线程安全的变量
    protected int hpMax;                        // 最大生命值

    protected int baseStrength;                  // 基础力量
    protected int baseArmature;                 // 基础护甲
    protected int baseConstitution;               // 基础体质
    protected int baseMagic;                       // 基础魔力
    protected int baseTechnique;                   // 基础技巧
    protected int baseSpeed;                         // 基础速度

    protected int baseHp;                            // 基础生命值

    protected int level;                           // 级别
    protected int star = 1;                            // 星级 初始1星 最大可强化到10星
    protected MonsterQualityEnum monsterQualityEnum;  // 作为野怪的级别
    /**
     * 当前经验值
     **/
    protected Integer currentExp;

    /**
     * 下一级所需经验值
     **/
    protected Integer nextLevelNeedExp;

    // 物理攻击 0 法术攻击 1
    private int attackType;

    // 毒抗
    private int poisonResistance;

    // 火抗
    private int flameResistance;

    // 电抗
    private int thunderResistance;

    // 冰抗
    private int iceResistance;

    // 暴击率加成
    private int criticalStrikeRateAdd;

    // 更好装备获得加成
    private int betterEquipmentRateAdd;

    // 闪避率加成
    private int dodgeChanceAdd;

    // 命中率 一般只有武器 或者套装才增加命中率
    private int hitRateAdd;

    // 1 前排 2 后排
    private int position;

    public Character() {

    }

    public Character(int id,String name, int level, int strength, int armature,
                     int constitution, int magic, int technique, int speed, int attackType, int poisonResistance,
                     int flameResistance, int thunderResistance, int iceResistance, int position) {
        this.id = id;
        this.name = name;
        this.level = level;
        this.strength = strength;
        this.armature = armature;
        this.constitution = constitution;
        this.hp = new AtomicInteger(this.constitution * 10);
        this.hpMax = this.hp.get();
        this.speed = speed;
        this.magic = magic;
        this.technique = technique;
//        this.skills = new ArrayList<>();
//        this.equips = new Equips();
//        this.states = new ArrayList<>();

        // 基础属性用于计算增幅型的技能
        this.baseStrength = strength;
        this.baseArmature = armature;
        this.baseConstitution = constitution;
        this.baseHp = this.constitution * 10;
        this.baseSpeed = speed;
        this.baseMagic = magic;
        this.baseTechnique = technique;

        this.attackType = attackType;
        this.currentExp = 0;
        this.nextLevelNeedExp = LevelExpEnum.getByLevel(level+1).getExp();

        this.poisonResistance = poisonResistance;
        this.flameResistance = flameResistance;
        this.thunderResistance = thunderResistance;
        this.iceResistance = iceResistance;
        this.position = position;
    }

    public Character(int id,String name, int strength, int armature, int constitution, int magic, int technique, int speed,
                     AtomicInteger hp, int hpMax, int baseStrength, int baseArmature, int baseConstitution, int baseMagic,
                     int baseTechnique, int baseSpeed, int baseHp, int level, MonsterQualityEnum monsterQualityEnum,
                     Integer currentExp, Integer nextLevelNeedExp, int attackType,
                     int poisonResistance, int flameResistance, int thunderResistance, int iceResistance, int position) {
        this.id = id;
        this.name = name;
        this.strength = strength;
        this.armature = armature;
        this.constitution = constitution;
        this.magic = magic;
        this.technique = technique;
        this.speed = speed;
        this.hp = hp;
        this.hpMax = hpMax;
        this.baseStrength = baseStrength;
        this.baseArmature = baseArmature;
        this.baseConstitution = baseConstitution;
        this.baseMagic = baseMagic;
        this.baseTechnique = baseTechnique;
        this.baseSpeed = baseSpeed;
        this.baseHp = baseHp;
        this.level = level;
        this.monsterQualityEnum = monsterQualityEnum;
        this.currentExp = currentExp;
        this.nextLevelNeedExp = nextLevelNeedExp;
        this.attackType = attackType;


        this.poisonResistance = poisonResistance;
        this.flameResistance = flameResistance;
        this.thunderResistance = thunderResistance;
        this.iceResistance = iceResistance;
        this.position = position;
    }

    public String string = "\t\t\t\t\t\t\t\t\t";

    //角色打印信息的重写
    public String prettyPrint() {
        return string + "*****信息****\n" +
                string + "姓名：" + this.name + "\n" +
                string + "级别" + this.level + "\n" +
                string + "血量：" + this.hp + "\n" +
                string + "力量：" + this.strength + "\n" +
                string + "法力：" + this.magic + "\n" +
                string + "技巧：" + this.technique + "\n" +
                string + "护甲值" + this.armature + "\n" +
                string + "速度" + this.speed + "\n" +
                string + "毒抗" + this.poisonResistance + "\n" +
                string + "火抗" + this.flameResistance + "\n" +
                string + "雷抗" + this.thunderResistance + "\n" +
                string + "冰抗" + this.iceResistance + "\n" +
                string + "当前经验" + this.currentExp + "\n" +
                string + "下一级经验" + this.nextLevelNeedExp + "\n" +
                string + "" + "*************";
    }


    //角色打印信息的重写
    public String toString() {
        return "**** 名称：" + this.name + " " +
                "级" + this.level + " " +
                "血：" + this.hp + " " +
                "甲: " + this.armature + " " +
                "力:" + this.strength + " " +
                "法:" + this.magic + " " +
                "技:" + this.technique + " " +
                "速:" + this.speed + " " +
                "抗性[毒:" + this.poisonResistance + " " +
                "火:" + this.flameResistance + " " +
                "雷:" + this.thunderResistance + " " +
                "冰:" + this.iceResistance + "] " +
                "经验(" + this.currentExp + "/" + this.nextLevelNeedExp + ") ****";
    }

    public String simplePrint() {
        return "**** 名称：" + this.name + " " +
                "级别" + this.level + " " +
                "血量:" + this.hp + " " +
                "护甲值:" + this.armature + " " +
                "力量:" + this.strength + " " +
                "法力:" + this.magic + " " +
                "技巧:" + this.technique + " " +
                "速度:" + this.speed + "****";
    }

    // 排版的方法:让内容都显示在屏幕的中间
    public static void typeset() {                 //排版的方法:让内容都显示在屏幕的中间
        System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
    }


    protected AttackResult simpleAttack(List<? extends Character> defenceList) {

        defenceList.sort(Comparator.comparing(Character::getCurrentHp));
        Character character = defenceList.get(0);
        int reduce = getStrength();
        character.hp.addAndGet(-reduce);

        String console = ConsoleColors.RED + getName() + ConsoleColors.RESET + "[" + getCurrentHp() + "/" + hpMax + "]"
                + " 攻击了" + ConsoleColors.BLUE + character.getName() + ConsoleColors.RESET
                + "，造成伤害" + reduce + "点";
        List<Character> characters = new ArrayList<>();
        if(character.getCurrentHp() <= 0) {
            //
            console += "," + character.getName() + "阵亡";
            // 如果是野怪要计算掉落的金币，物品和经验
            characters.add(character);
        }

        return new AttackResult(true,console,characters);
    }

    public abstract int getDeadExp();

    // 穿装备
    @Override
    public Tuple<String, List<String>> wear(Equipment equipment, int weaponType) {
        Tuple<String, List<String>> result = new Tuple<>();
        result.setValue(new ArrayList<>());

        String takeOffEquipmentId = null;
        // 装备级别要小于等于英雄的等级
        if(equipment.getLevel() > 1 && equipment.getLevel() > getLevel()){
            result.getValue().add("对不起，装备等级高于英雄，无法穿戴");
            result.setKey(null);
            return result;
        }
        // 先看同类的是否存在，如果存在，先脱再穿
        EquipmentEnum equipmentPosition = equipment.getEquipmentPosition();
        switch (equipmentPosition.getCode().toLowerCase()) {
            case "weapon":
                if(weaponType != equipment.getWeaponTypeEnum().getCode()){
                    result.getValue().add("对不起，该武器不适合英雄，无法穿戴");
                    result.setKey(null);
                    return result;
                }
                if (equips.getWeapon() != null) {
                    takeOffEquipmentId = takeOff(equips.getWeapon());
                }
                equips.setWeapon(equipment);
                break;
            case "breastplate":
                if (equips.getBreastplate() != null) {
                    takeOffEquipmentId = takeOff(equips.getBreastplate());
                }
                equips.setBreastplate(equipment);
                break;
            case "shield":
                if (equips.getShield() != null) {
                    takeOffEquipmentId = takeOff(equips.getShield());
                }
                equips.setShield(equipment);
                break;
            case "shoes":
                if (equips.getShoes() != null) {
                    takeOffEquipmentId = takeOff(equips.getShoes());
                }
                equips.setShoes(equipment);
                break;
            case "helmet":
                if (equips.getHelmet() != null) {
                    takeOffEquipmentId = takeOff(equips.getHelmet());
                }
                equips.setHelmet(equipment);
                break;
            case "gloves":
                if (equips.getGlove() != null) {
                    takeOffEquipmentId = takeOff(equips.getGlove());
                }
                equips.setGlove(equipment);
                break;
            case "necklace":
                if (equips.getNecklace() != null) {
                    takeOffEquipmentId = takeOff(equips.getNecklace());
                }
                equips.setNecklace(equipment);
                break;
            case "leftring":
                if (equips.getLeftRing() != null) {
                    takeOffEquipmentId = takeOff(equips.getLeftRing());
                }
                equips.setLeftRing(equipment);
                break;
            case "rightring":
                if (equips.getRightRing() != null) {
                    takeOffEquipmentId = takeOff(equips.getRightRing());
                }
                equips.setRightRing(equipment);
                break;
            case "legarmor":
                if (equips.getLegArmor() != null) {
                    takeOffEquipmentId = takeOff(equips.getLegArmor());
                }
                equips.setLegArmor(equipment);
                break;
            case "belt":
                if (equips.getBelt() != null) {
                    takeOffEquipmentId =  takeOff(equips.getBelt());
                }
                equips.setBelt(equipment);
                break;
            case "barcer":
                if (equips.getBarcer() != null) {
                    takeOffEquipmentId = takeOff(equips.getBarcer());
                }
                equips.setBarcer(equipment);
                break;
            case "shoulder":
                if (equips.getShoulder() != null) {
                    takeOffEquipmentId = takeOff(equips.getShoulder());
                }
                equips.setShoulder(equipment);
                break;
            default:
                result.getValue().add("穿戴装备失败" + equipment.getName());
                result.setKey(null);
                return result;
        }
        List<String> lines = wearEquipment(equipment);
        result.getValue().addAll(lines);
        result.setKey(takeOffEquipmentId);
        return result;
    }

    @Override
    public String takeOff(Equipment equipment) {
        // 脱装备就是整体属性下降
        this.setStrength(this.strength - equipment.getStrength());
        this.setArmature(this.armature - equipment.getArmature());
        this.setConstitution(this.constitution - equipment.getConstitution());
        this.hp.addAndGet(-equipment.getHp());
        this.setMagic(this.magic - equipment.getMagic());
        this.setTechnique(this.technique - equipment.getTechnique());
        this.setSpeed(this.speed - equipment.getSpeed());
        this.setPoisonResistance(this.poisonResistance - equipment.getPoisonResistance());
        this.setFlameResistance(this.flameResistance - equipment.getFlameResistance());
        this.setThunderResistance(this.thunderResistance - equipment.getThunderResistance());
        this.setIceResistance(this.iceResistance - equipment.getIceResistance());
        equipment.setUsed(false);
        return equipment.getId();
    }

    private List<String> wearEquipment(Equipment equipment) {
        List<String> result = new ArrayList<>();
        result.add("穿戴装备" + equipment.getName() + " 成功");
        // 穿装备就是整体属性提升
        this.setStrength(this.strength + equipment.getStrength());
        this.setArmature(this.armature + equipment.getArmature());
        this.setConstitution(this.constitution + equipment.getConstitution());
        this.hp.addAndGet(equipment.getConstitution() * 10 + equipment.getHp());
        this.hpMax = this.hp.get();
        this.setMagic(this.magic + equipment.getMagic());
        this.setTechnique(this.technique + equipment.getTechnique());
        this.setSpeed(this.speed + equipment.getSpeed());
        // 装备提升的抗性
        this.setThunderResistance(this.thunderResistance + equipment.getThunderResistance());
        this.setFlameResistance(this.flameResistance + equipment.getFlameResistance());
        this.setPoisonResistance(this.poisonResistance + equipment.getPoisonResistance());
        this.setIceResistance(this.iceResistance + equipment.getIceResistance());
        equipment.setUsed(true);
        return result;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setStrength(int strength) {
        this.strength = strength;
    }

    public void setConstitution(int constitution) {
        this.constitution = constitution;
    }

    public void setMagic(int magic) {
        this.magic = magic;
    }

    public void setTechnique(int technique) {
        this.technique = technique;
    }

    public void setArmature(int armature) {
        this.armature = armature;
    }

    public int getArmature() {
        return armature;
    }

//    public void setHp(int hp) {
//        this.hp = new AtomicInteger(hp);
//    }


    public String getName() {
        return name;
    }

    public int getStrength() {
        return strength;
    }

    public int getConstitution() {
        return constitution;
    }

    public int getMagic() {
        return magic;
    }

    public int getTechnique() {
        return technique;
    }

    public AtomicInteger hp() {
        return hp;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public Integer getCurrentExp() {
        return currentExp;
    }

    public void setCurrentExp(Integer currentExp) {
        this.currentExp = currentExp;
    }

    public Integer getNextLevelNeedExp() {
        return nextLevelNeedExp;
    }

    public int setNextLevelNeedExp() {
        // 需要的经验值越来越高
        this.nextLevelNeedExp = LevelExpEnum.getByLevel(level).getExp();
        return this.nextLevelNeedExp;
    }

    public MonsterQualityEnum getMonsterQualityEnum() {
        return monsterQualityEnum;
    }

    public void setMonsterQualityEnum(MonsterQualityEnum monsterQualityEnum) {
        this.monsterQualityEnum = monsterQualityEnum;
    }

    public abstract void levelUp(int exp);

    public int getBaseStrength() {
        return baseStrength;
    }

    public int getBaseArmature() {
        return baseArmature;
    }

    public int getBaseConstitution() {
        return baseConstitution;
    }

    public int getBaseMagic() {
        return baseMagic;
    }

    public int getBaseTechnique() {
        return baseTechnique;
    }

    public int getBaseSpeed() {
        return baseSpeed;
    }

    public int getBaseHp() {
        return baseHp;
    }

    public void setBaseStrength(int baseStrength) {
        this.baseStrength = baseStrength;
    }

    public void setBaseArmature(int baseArmature) {
        this.baseArmature = baseArmature;
    }

    public void setBaseConstitution(int baseConstitution) {
        this.baseConstitution = baseConstitution;
    }

    public void setBaseMagic(int baseMagic) {
        this.baseMagic = baseMagic;
    }

    public void setBaseTechnique(int baseTechnique) {
        this.baseTechnique = baseTechnique;
    }

    public void setBaseSpeed(int baseSpeed) {
        this.baseSpeed = baseSpeed;
    }

    public void setBaseHp(int baseHp) {
        this.baseHp = baseHp;
    }

    public int getHpMax() {
        return hpMax;
    }

    public int getCurrentHp() {
        return hp.get();
    }

    public int getAttackType() {
        return attackType;
    }

    public void setAttackType(int attackType) {
        this.attackType = attackType;
    }

    public AtomicInteger getHp() {
        return hp;
    }

    public void setHp(AtomicInteger hp) {
        this.hp = hp;
    }

    public void setHpMax(int hpMax) {
        this.hpMax = hpMax;
    }

    public void setNextLevelNeedExp(Integer nextLevelNeedExp) {
        this.nextLevelNeedExp = nextLevelNeedExp;
    }

    public int getStar() {
        return star;
    }

    public void setStar(int star) {
        this.star = star;
    }

    public int getPoisonResistance() {
        return poisonResistance;
    }

    public void setPoisonResistance(int poisonResistance) {
        this.poisonResistance = poisonResistance;
    }

    public int getFlameResistance() {
        return flameResistance;
    }

    public void setFlameResistance(int flameResistance) {
        this.flameResistance = flameResistance;
    }

    public int getThunderResistance() {
        return thunderResistance;
    }

    public void setThunderResistance(int thunderResistance) {
        this.thunderResistance = thunderResistance;
    }

    public int getIceResistance() {
        return iceResistance;
    }

    public void setIceResistance(int iceResistance) {
        this.iceResistance = iceResistance;
    }


    public int getCriticalStrikeRateAdd() {
        return criticalStrikeRateAdd;
    }

    public void setCriticalStrikeRateAdd(int criticalStrikeRateAdd) {
        this.criticalStrikeRateAdd = criticalStrikeRateAdd;
    }

    public int getBetterEquipmentRateAdd() {
        return betterEquipmentRateAdd;
    }

    public void setBetterEquipmentRateAdd(int betterEquipmentRateAdd) {
        this.betterEquipmentRateAdd = betterEquipmentRateAdd;
    }

    public int getDodgeChanceAdd() {
        return dodgeChanceAdd;
    }

    public void setDodgeChanceAdd(int dodgeChanceAdd) {
        this.dodgeChanceAdd = dodgeChanceAdd;
    }

    public int getHitRateAdd() {
        return hitRateAdd;
    }

    public void setHitRateAdd(int hitRateAdd) {
        this.hitRateAdd = hitRateAdd;
    }

    public int getId() {
        return id;
    }

    // 转职
    public void transfer(JobTemplate jobTemplate) throws IOException, ClassNotFoundException {
        // 首先要替换到自己的模板



    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public Equips getEquips() {
        if(equips == null){
            equips = new Equips();
        }
        return equips;
    }

    public void setEquips(Equips equips) {
        this.equips = equips;
    }
}
